package com.wt.struggle.goodbye.user.activity

import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.support.v7.widget.LinearLayoutManager
import com.wt.struggle.goodbye.App
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.*
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.ProUserInfo
import com.wt.struggle.goodbye.user.adapter.UserTitleAdapter
import kotlinx.android.synthetic.main.head.*
import kotlinx.android.synthetic.main.set_layout.*
import org.json.JSONObject

class SetActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

        when (what) {
            Send.LOGIN_OUT -> {

                if (netInfo.code == Config.SUCCESS) {

                    Share.clear(this)

                    App.exit()

                    val intent=Intent(this, LoginActivity::class.java)
                    intent.putExtra("code",2)
                    startActivity(intent)


                }
            }
        }

    }

    var userInfo: ProUserInfo? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.set_layout)

        userInfo = intent.getParcelableExtra("userInfo")

        tvTitle.text = "设置"
        initSetAdapter()
        initClick()
    }

    private fun initClick() {
        imageBack.setOnClickListener {
            finish()
        }

        buttonLogin.setOnClickListener {
            exitLogin()
        }
    }

    private fun exitLogin() {
        val build = AlertDialog.Builder(this)
        build.setTitle("提示").setMessage("是否退出当前登录账号?").setPositiveButton("退出") { dialog, _ ->

            send!!.loginOut(handler)

        }.setNegativeButton("取消") { _, _ -> }.show()
    }

    val titleList = arrayListOf<String>("切换账号", "账号安全", "消息推送", "关于目趣")

    val picList =
        arrayListOf<Int>(R.drawable.set_1, R.drawable.set_2, R.drawable.set_3, R.drawable.set_5)

    private fun initSetAdapter() {
        setRecyclerView.layoutManager = LinearLayoutManager(this)
        val adapter = UserTitleAdapter(this, titleList, picList, 2)

        setRecyclerView.adapter = adapter

        adapter.itemClickListener = object : ItemClickListener {
            override fun onItemClick(position: Int) {
                when (position) {
                    0 -> {
                        startActivity(Intent(this@SetActivity, ChangeAccountActivity::class.java))
                    }

                    1 -> {
                        val intent = Intent(this@SetActivity, AccountSafeActivity::class.java)
                        intent.putExtra("userInfo", userInfo)
                        startActivityForResult(intent, 5566)
                    }

                    2 -> {
                    }

                    3 -> {
                        startActivity(Intent(this@SetActivity, AboutActivity::class.java))

                    }

//                    4 -> {
//                        startActivity(Intent(this@SetActivity, ChangeTextSizeActivity::class.java))
//
//                    }

                }

            }

            override fun onLongClick(position: Int) {

            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 5566 && resultCode == Activity.RESULT_OK) {
            val email = data!!.getStringExtra("email")
            val phone = data.getStringExtra("phone")
            userInfo!!.email = email
            userInfo!!.mobile = phone
        }
    }
}